// history-order-list.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_offset: 0,
    page_size: 10,
    is_load_all: false,
    showNoData:false,
    merchants: [
  
    ]
  },
  openRate: function (event){
    var sn = event.currentTarget.dataset.sn;
 
    wx.navigateTo({
      url: "/pages/evaluate/evaluate?sn=" + sn
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    app.http.request({
      url: '/api/v1/user/orders?offset=' + this.data.page_offset * this.data.page_size,
      method: 'GET',
      data: {},

      success: function (res) {
        console.log(res.data.data)
        if (res.data.code == '200' && res.data.data['merchants']) {
          that.setData({
            merchants: res.data.data.merchants,
            showNoData: res.data.data.merchants.length <= 0
          })
        }
      },
      fail: function () {

      }
    })
  },
  doRate: function (event){
    var id = event.currentTarget.dataset.id
    app.http.request({
      url:'/api/v1/merchant/rate',
      data:{
        id:id
      },
      success: function(res){

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  bindscrolltolower: function () {
    var offset = this.data.page_offset
    offset++
    this.setData({
      page_offset: offset
    })
    console.log(offset)
    var path = '/api/v1/user/orders?offset=' + this.data.page_offset * this.data.page_size
    var that = this
    getApp().http.request({
      url: path, //仅为示例，并非真实的接口地址
      method: 'GET',
      data: {},
      dataType: 'json',
      success: function (res) {
        console.log(res)
        if (res.data.data.list.length > 0) {
          var tmp_listdata = that.data.merchants
          for (var i = 0; i < res.data.data.merchants.length; i++) {
            tmp_listdata.push(res.data.data.merchants[i])
          }
          that.setData({
            categories: res.data.data.categories,
            merchants: tmp_listdata
          })
        } else {
          if (!that.data.is_load_all) {
            wx.showToast({
              title: '没有更多数据了',
                duration:2000
            })
            that.setData({
              is_load_all: true
            })
          }
        }
      },
      fail: function () {

      }
    })
  }
})